

let gameDebug = {};

// 显示图片占用的内存
gameDebug.showImgMem = function(){
    // function getInfo(asset,targetArr){
    //     if(Array.isArray(asset)){
    //         for(let i = 0; i < asset.length; ++i){
    //             let key = asset[i];
    //             getInfo(key,targetArr);
    //         }
    //     }else if(asset){
    //         // cc.log('typeof asset:',(typeof asset));
    //         let id = asset.id;//cc.loader._getReferenceKey(asset);
    //         let item = cc.loader.getItem(id);
    //         if(item){
    //             asset = item.content;
    //             if(asset instanceof cc.Asset){
    //                 let nativeUrl = asset.nativeUrl;
    //                 if(nativeUrl){
    //                     targetArr.push(asset);
    //                 }
    //             }
    //         }
    //     }
    // }

    // code
    let cache = cc.assetManager.assets._map;

    let totalBytes = 0;
    let tex2dCount = 0;
    for(let k in cache){
        let item = cache[k];
        if(item instanceof cc.Texture2D){
            let w = item.width;
            let h = item.height;
            totalBytes += (w * h * 4);
            tex2dCount++;
        }
    }
    console.log('tex2dCount:',tex2dCount);
    // console.log('targetArr:',targetArr);
    // 计算

    // targetArr.forEach( item =>{
    //     if(item){
    //         if(item instanceof cc.Texture2D){
    //             let w = item.width;
    //             let h = item.height;
    //             totalBytes += (w * h * 4);
    //         }
    //     }
    // });
    let kb = Math.floor(totalBytes/1024);
    console.log('图片总内存:',kb, ' kb');
};

//=========================================================
// debug, 显示当前场景所有的节点个数
gameDebug.showNodeCount = function(bActive = true){ // eslint-disable-line
    let scene = cc.director.getScene();
    let lstCur = [scene];
    let lstNext = [];
    let allNodes = [];
    while (lstCur.length > 0){
        for(let i = 0; i < lstCur.length; ++i){
            let n = lstCur[i];
            allNodes.push(n);
            n.children.forEach(function (child) {
                if(child.active && child.opacity > 0){
                    lstNext.push(child);
                }
            });
        }
        lstCur = lstNext;
        lstNext = [];
    }
    cc.log('总节点个数:',allNodes.length);
};


gameDebug.throwUnreachableError = function(){
    if(CC_DEV){
        throw new Error(`不应该执行到这里`);
    }
};

window.gameDebug = gameDebug;
module.exports = gameDebug;
